package yana.tpe.onlineshop.services.impl;

import java.io.Serializable;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import yana.tpe.onlineshop.domain.dao.AbstractDao;
import yana.tpe.onlineshop.domain.domains.AbstractEntity;
import yana.tpe.onlineshop.services.AbstractService;

/**
 *
 */

@Transactional
public abstract class AbstractServiceImpl
        <Key extends Serializable, Value extends AbstractEntity<Key>>
        implements AbstractService <Key, Value> {

    protected final Logger logger = LoggerFactory.getLogger(getClass());
    
    @Override
    @Transactional
    public Value get(Key id) {
        logger.debug("Get by id {}.", id);
        Value get = getDao().get(id);
        logger.debug("Return {}.", get);
        return get;
    }

    @Override
    @Transactional
    public List<Value> getList() {
        logger.debug("Get list.");
        List<Value> list = getDao().list();
        logger.debug("Return {}.", list);
        return list;
    }

    @Override
    @Transactional
    public Key save(Value object) {
        logger.debug("Save new object {}.", object);
        Key save = getDao().save(object);
        logger.debug("New object id {}.", save);
        return save;
    }

    @Override
    @Transactional
    public void saveOrUpdate(Value object) {
        logger.debug("Update object {}.", object);
        getDao().saveOrUpdate(object);
    }

    @Override
    @Transactional
    public void delete(Value object) {
        logger.debug("Update object {}.", object);
        getDao().delete(object);
    }

    protected abstract AbstractDao<Key, Value> getDao();
}
